import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.*;
import javax.swing.border.EmptyBorder;


public class TaxUI extends JFrame {

	private JPanel contentPane;
	private Tax tax;
	private Player p;
	private Sound sound; 
	CommandWithReturn bankruptcyCmd = new BankruptcyCheckerCommand();
	CommandWithInt cmdInt = new PropertySelectionUICommand();
	Command transferCmd = new TransferAssetsCommand();

	/**
	 * Create the frame.
	 */
	public TaxUI(Tax tax, Player p, String pathForSound) {
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 325, 460);
		contentPane =  new ImagePanel(new ImageIcon(tax.getTaxImage()).getImage());
		this.setTitle(tax.getName());
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		this.p = p;
		this.tax = tax;
		
		ImageIcon exitPic = new ImageIcon("Graphics/ReturnToGame.png");
		ImageIcon exitPicOver = new ImageIcon("Graphics/ReturnToGameOver.png");
		JButton exitButton = new JButton(exitPic);
		
		contentPane.add(exitButton);
		
		exitButton.setBounds(240, 10, 62, 28);
		
		exitButton.setBorderPainted(false);
		exitButton.setContentAreaFilled(false);
		exitButton.setOpaque(false);
		exitButton.setRolloverEnabled(true);
		exitButton.setRolloverIcon(exitPicOver);
		exitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitButtonPressed(evt);
            }
        });
		this.setVisible(true);
		
		this.sound = new Sound();//sounds
		this.sound.playLoop(pathForSound);
		
	}
	
	private void exitButtonPressed(java.awt.event.ActionEvent e){
		
		 if(p.getCurrentMoney()<this.tax.getCost()){
			 bankruptcyCmd.execute();
			 if(bankruptcyCmd.execute() == 0){
			 JOptionPane.showMessageDialog(null, "Wala ka nang pera kahit ibenta mo pa lahat ng pag-aari mo. Natalo ka sa laro, sir.");
			 transferCmd.execute();
			 this.p.setActive(false);
			 }else{
			 JOptionPane.showMessageDialog(null, "Wala ka nang pera pero pwede mong ibenta ang mga pag-aari mo.");
			 cmdInt.execute(2);
			 }
		 } else{
			 p.subtractMoney(this.tax.getCost());
			 JOptionPane.showMessageDialog(null, "Nagbayad ka ng " + this.tax.getCost() + "Php. Ang pera mo na ngayon ay " + p.getCurrentMoney() + "Php.");
		 }
		
		
		NextPlayerCommand nextPlayerCommand = new NextPlayerCommand();
		nextPlayerCommand.execute();
		this.dispose();
		this.sound.getClip().stop();
		
	}	
	
}